package com.blueocean_health.care.management.controller;

import com.alibaba.fastjson.JSONObject;
import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.checkWorkVo.*;
import com.blueocean_health.care.management.domain.dto.SelectNurseDto;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.exception.OrderException;
import com.blueocean_health.care.management.service.CheckWorkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.Collator;
import java.util.*;

/**
 * @Author ChenPeng cp
 * @Date 2018/4/26 14:22
 */

@RestController
@RequestMapping(value = "/web/checkWork", produces = MediaType.APPLICATION_JSON_VALUE)
public class CheckWorkController {

    private static Logger logger= LoggerFactory.getLogger(CheckWorkController.class);

    @Autowired
    private CheckWorkService checkWorkService;

    //小组下拉列表
    @RequestMapping("/groupList/query")
    public SysResult getNurseGroup() throws OrderException {
        User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
        String hid =  courrentUser.getHospitalId();
        SysResult sysResult = checkWorkService.queryNurseGroupListByHid(hid);
        return sysResult;
    }

    //添加人员列表
    @RequestMapping("/workerList")
    public SysResult queryNurseByHid(@RequestBody SelectNurseDto selectNurseDto) throws OrderException {
        User user = LocalManager.getVal(LocalManagerType.USER_WEB);
        String hid = user.getHospitalId();
        selectNurseDto.setHospital(hid);
        return checkWorkService.queryNurseByHid(selectNurseDto);
    }

    //考勤列表查询
    @RequestMapping("/list")
    public SysResult queryCheckWorkList(@RequestBody CheckWorkListDto checkWorkListDto) throws OrderException {
        User user = LocalManager.getVal(LocalManagerType.USER_WEB);
        String hid = user.getHospitalId();
        checkWorkListDto.setHid(hid);
        return checkWorkService.queryCheckWorkList(checkWorkListDto,null);
    }

    //考勤列表excel导出
    @RequestMapping("/list/excel")
    public void queryCheckWorkListExcel(CheckWorkListDto checkWorkListDto, HttpServletResponse response) throws Exception{
        User user = LocalManager.getVal(LocalManagerType.USER_WEB);
        String hid = user.getHospitalId();
        checkWorkListDto.setHid(hid);
        List<CheckWorkListVo> list = checkWorkService.queryCheckWorkListExcel(checkWorkListDto);
        String finalFileName = null;
        try {
            finalFileName = URLEncoder.encode("考勤管理_"+ TimeUtils.getTime(TimeType.yyyyMMdd)+".xls", "UTF8");
        } catch (Exception e) {
            finalFileName  = "nurse_check_work"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls";
        }
        response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        ExcelUtil.getInstance().exportObj2Excel(toClient, list, CheckWorkListVo.class);
    }

    //点击编辑考勤后的护工列表
    @RequestMapping("/groupWorkers/query")
    public SysResult queryCheckWorkByGroup(@RequestBody CheckWorkListDto checkWorkListDto) throws OrderException {
        User user = LocalManager.getVal(LocalManagerType.USER_WEB);
        String hid = user.getHospitalId();
        checkWorkListDto.setHid(hid);
        checkWorkListDto.setToday(checkWorkListDto.getCheckDate());
        return checkWorkService.queryCheckWorkList(checkWorkListDto,"1");
    }

    //编辑考勤
    @RequestMapping("/update")
    public SysResult updateCheckWork(@RequestBody UpdateCheckWorkVo vos) throws OrderException {
        User user = LocalManager.getVal(LocalManagerType.USER_WEB);
        return checkWorkService.updateCheckWorkByGroup(vos);
    }

    //查看月度考勤
    @RequestMapping("/worker/query")
    public SysResult queryMonthCheckByWorker(@RequestBody SelectMonthDto dto) throws OrderException {
        return checkWorkService.queryMonthCheckByWorker(dto);
    }


    /**
     * 批量修改考勤记录
     * @return
     */
    @RequestMapping("/update-batch")
    public SysResult updateCheckWorkBatch(@RequestBody String data) throws OrderException {
        logger.info("批量修改考勤记录，获取请求数据："+data);
        JSONObject uobj=null;
        try {
            uobj=JSONObject.parseObject(data);
        }catch (Exception ex){
            logger.error("无效的考勤修改数据,"+ex.getMessage());
            throw new OrderException("无效的请求数据：考勤记录批量修改");
        }
        checkWorkService.updateCheckWorkBatch(uobj);
        return SysResult.ok();
    }


    //小组护工下拉列表
    @RequestMapping("/group/workerList")
    public SysResult queryWorkerList(@RequestBody String jsonString) throws OrderException {
        JSONObject jsonObject =  JSONObject.parseObject(jsonString);
        String workId = (String) jsonObject.get("workId");
        User user = LocalManager.getVal(LocalManagerType.USER_WEB);
        String hid = user.getHospitalId();
        return checkWorkService.queryWorkerListForCheckWork(workId,hid);
    }



    //新 考勤列表查询
    @RequestMapping("/listNew")
    public SysResult queryCheckWorkListNew(@RequestBody CheckWorkListNewDto checkWorkListNewDto) throws OrderException {
        User user = LocalManager.getVal(LocalManagerType.USER_WEB);
        String hid = user.getHospitalId();
        checkWorkListNewDto.setHid(hid);
        return checkWorkService.queryCheckWorkListNew(checkWorkListNewDto);
    }

    //新 考勤列表excel导出
    @RequestMapping("/list/excelNew")
    public void queryCheckWorkListExcelNew(CheckWorkListNewDto checkWorkListNewDto, HttpServletResponse response) throws Exception{
        User user = LocalManager.getVal(LocalManagerType.USER_WEB);
        String hid = user.getHospitalId();
        checkWorkListNewDto.setHid(hid);
        List<CheckWorkListNewVo> list = checkWorkService.queryCheckWorkListExcelNew(checkWorkListNewDto);
        String finalFileName = null;
        try {
            finalFileName = URLEncoder.encode("考勤管理_"+ TimeUtils.getTime(TimeType.yyyyMMdd)+".xls", "UTF8");
        } catch (Exception e) {
            finalFileName  = "nurse_check_work"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls";
        }
        response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        ExcelUtil.getInstance().exportObj2Excel(toClient, list, CheckWorkListNewVo.class);
    }

    @RequestMapping("/updateNew")
    public SysResult updateCheckWorkNew(@RequestBody UpdateCheckWorkNewVo updateCheckWorkNewVo) throws OrderException{
        return checkWorkService.updateCheckWorkNew(updateCheckWorkNewVo);
    }

}
